﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AFT.Parking.Foundation;
using System.Web.Mvc;

namespace AFT.Parking.Foundation.Web
{

    /// <summary>
    /// 分页类
    /// </summary>
    public class Pager
    {
        protected int _pageIndex;
        protected int _pageSize;
        protected int _totalCount;
        protected string _urlFormat;
        protected string _curClass;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="paged"></param>
        /// <param name="urlFormat">url中页码用{0}替换</param>
        /// <param name="curClass">表示当前页的class</param>
        public Pager(IPaged paged, string urlFormat, string curClass)
        {
            _pageIndex = paged.PageIndex;
            _pageSize = paged.PageSize;
            _totalCount = paged.TotalCount;
            _urlFormat = urlFormat;
            _curClass = curClass;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="urlFormat">url中页码用{0}替换</param>
        /// <param name="curClass">表示当前页的class</param>
        public Pager(int pageIndex, int pageSize, int totalCount, string urlFormat, string curClass)
        {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
            _totalCount = totalCount;
            _urlFormat = urlFormat;
            _curClass = curClass;
        }

        public MvcHtmlString Html
        {
            get
            {
                return BuildHtml(_pageIndex, _pageSize, _totalCount, _urlFormat);
            }
        }

        private MvcHtmlString BuildHtml(int pageIndex, int pageSize, int totalCount, string urlFormat)
        {
            var totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

            var sb = new StringBuilder();
            sb.AppendFormat("<div class=\"p-pageBar\"><em><a href=\"{0}\">首页</a></em>", string.Format(urlFormat, 1));

            if (pageIndex == 1)
            {
                sb.AppendFormat("<em>上一页</em>");
            }
            else
            {
                sb.AppendFormat("<em><a href=\"{0}\">上一页</a></em>", string.Format(urlFormat, pageIndex - 1));
            }

            sb.Append("<div>");
            //当前页小于10，显示1到10页
            if (pageIndex <= 10)
            {
                var p = totalPage > 10 ? 10 : totalPage;
                for (int i = 1; i <= p; i++)
                {
                    var cur = pageIndex == i ? _curClass : "";
                    sb.AppendFormat("<span class=\"{2}\"><a href=\"{0}\">{1}</a></span>", string.Format(urlFormat, i), i, cur);
                }
                if (totalPage > 10)
                {
                    sb.Append("...");
                }
            }
            else //分页数大于10
            {
                sb.Append("...");
                //显示当前页前后五页，若已到最后10页，显示最后10页
                var p1 = (pageIndex + 4) >= totalPage ? totalPage - 10 : pageIndex - 5;
                var p2 = (pageIndex + 4) >= totalPage ? totalPage : pageIndex + 4;
                for (int i = p1; i <= p2; i++)
                {
                    var cur = pageIndex == i ? _curClass : "";
                    sb.AppendFormat("<span class=\"{2}\"><a href=\"{0}\">{1}</a></span>", string.Format(urlFormat, i), i, cur);
                }
                if (pageIndex + 4 < totalPage)
                {
                    sb.Append("...");
                }
            }
            sb.Append("</div>");

            if (pageIndex == totalPage)
            {
                sb.AppendFormat("<em>下一页</em>");
            }
            else
            {
                sb.AppendFormat("<em><a href=\"{0}\">下一页</a></em>", string.Format(urlFormat, pageIndex + 1));
            }

            sb.AppendFormat("<em><a href=\"{0}\">末页</a></em></div>", string.Format(urlFormat, totalPage));
            return new MvcHtmlString(sb.ToString());
        }
    }

    /// <summary>
    /// 分页方法
    /// </summary>
    public static class PageHelper
    {
        public static Pager Pager(IPaged paged, string urlFormat, string curClass)
        {
            return new Pager(paged, urlFormat, curClass);
        }

        public static Pager Pager(int pageIndex, int pageSize, int totalCount, string urlFormat, string curClass)
        {
            return new Pager(pageIndex, pageSize, totalCount, urlFormat, curClass);
        }
    }
}

